package managers;

import java.sql.SQLException;
import java.util.ArrayList;
import util.Codes;
import business.Company;

public class MngCompanies extends Manager {
	
	public MngCompanies() {
		
	}
	
	public Company getCompany(int cmpyID) {
		Company cmpy = new Company();
		
		String sql = "SELECT name, address, location FROM companies WHERE id = " + cmpyID;
		
		try {
			rst = stmt.executeQuery(sql);
			if (rst.next()) {
				cmpy.setId(cmpyID);
				cmpy.setName(rst.getString("name"));
				cmpy.setAddress(rst.getString("address"));
				cmpy.setLocation(rst.getBoolean("location"));
			}
			//cnx.getConn().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return cmpy;
	}
	
	public ArrayList<Company> getAllCompany() {
		ArrayList<Company> companies = new ArrayList<Company>();
		
		String sql = "SELECT id, name, address, location FROM companies WHERE id != 0 ORDER BY name";
		
		try {
			rst = stmt.executeQuery(sql);
			
			while (rst.next()) {
				Company cmpy = new Company();
				cmpy.setId(rst.getInt("id"));
				cmpy.setName(rst.getString("name"));
				cmpy.setAddress(rst.getString("address"));
				cmpy.setLocation(rst.getByte("location"));
				
				companies.add(cmpy);
			}
			//cnx.getConn().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return companies;
	}
	
	public boolean saveCompany(int id, String name, String address, byte location) {
		Company cmpy = new Company();
		cmpy.setId(id);
		cmpy.setName(name);
		cmpy.setAddress(address);
		cmpy.setLocation(location);
		
		return saveCompany(cmpy);
	}
	
	public boolean saveCompany(Company cmpy) {
		boolean res = false;
		
		String sql;
		
		if (cmpy.getId() > 0) {
			sql = "UPDATE companies SET name = '" + cmpy.getName() + "', address = '" + cmpy.getAddress() + "', " +
				  "location = " + cmpy.getLocation() + " WHERE id = " + cmpy.getId();
		}	else {
			sql = "INSERT INTO companies (name, address, location) VALUES ('" + cmpy.getName() + "'" + 
				  ", '" + cmpy.getAddress() + "', ";
			if (cmpy.getLocation() == false) sql += "0";
			else if (cmpy.getLocation() == true) sql += "1";
			
			sql += ")";
			
			//System.out.print(sql);
		}
		
		try {
			if (stmt.executeUpdate(sql) > 0) res = true;
			
			//cnx.getConn().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return res;
	}
	
	public boolean delCompany(int cmpyID) {
		boolean res = false;
		
		String sql = "DELETE companies.* FROM companies WHERE id = " + cmpyID;
		
		try {
			if (stmt.executeUpdate(sql) > 0) res = true;
			
			//cnx.getConn().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public String getTable() {
		String sql, tbl = "";
		
		sql = "	SELECT id, name, IF (location = 0, 'Lima', 'Provincia') AS location " +
			  "	FROM companies " +
			  "	WHERE id > 0";
		
		try {
			rst = stmt.executeQuery(sql);
			while (rst.next()) {
				tbl +=	"<tr>" + 
							"<td>" + Codes.getCode(rst.getString("id"), 2) + "</td>" +
							"<td>" + rst.getString("name") + "</td>" +
							"<td>" + rst.getString("location") + "</td>" +
							"<td class=\"tdButt\">" +
								"<a href=\"JavaScript:;\" title=\"Modificar\" onclick=\"editCompany('" + rst.getInt("id") + "');\">" +
									"<img src=\"imgs/page_white_edit.png\" alt=\"Modificar\">" +
								"</a>" +
							"</td>" +
							"<td class=\"tdButt\">" +
								"<a href=\"JavaScript:;\" title=\"Eliminar\" onclick=\"delCompany(this, '" + rst.getInt("id") + "');\">" +
									"<img src=\"imgs/button_cancel.png\" alt=\"Eliminar\">" +
								"</a>" +
							"</td>" +	
						"</tr>";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return tbl;
	}
}